package com.yisu.auction.api.core.service.impl;

import com.yisu.auction.api.core.mapper.MemberMapper;
import com.yisu.auction.api.core.mapper.SmsMapper;
import com.yisu.auction.api.core.service.IJPushService;
import com.yisu.auction.api.core.service.impl.func.JPushUtil;
import com.yisu.auction.api.core.service.impl.func.SmsPushUtil;
import com.yisu.auction.api.domain.vo.jpushPackageVos.JPushConfigVo;
import com.yisu.auction.ewf.entity.Member;
import com.yisu.auction.ewf.entity.Sms;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * author: 赵杰
 * Business Description：极光推送业务接口实现
 * createTime：2017/9/13
 */
@Service("IJPushService")
public class JPushServiceImpl extends SmsPushUtil implements IJPushService {
    @Resource
    private SmsMapper smsMapper;
    @Resource
    private MemberMapper memberMapper;

    /**
     * author: 赵杰
     * Business Description：定时推送业务
     *
     * @return
     */
    @Override
    public boolean timingPush() {
        final List<Sms> pushSmsVos = smsMapper.getPushListInTenMinute();
        if (!CollectionUtils.isEmpty(pushSmsVos)) {
            //另起线程，异步执行
            ExecutorService executorService = Executors.newFixedThreadPool(1);
            Runnable syncRunnable = new Runnable() {
                @Override
                public void run() {
                    for (Sms sms : pushSmsVos) {
                        Member member = memberMapper.selectByPrimaryKey(sms.getMemberId());
                        if (member == null) {
                            continue;
                        }
                        JPushConfigVo pushConfigVo2 = getJPushConfigByBingId(member.getAppSign());
                        JPushUtil.sendWinMsg(pushConfigVo2.getAppKey(), pushConfigVo2.getMasterSecret(), sms.getMemberId(), sms.getPrizeId(), sms.getTerm(), sms.getContent());
                        Sms updateModel = new Sms();
                        updateModel.setId(sms.getId());
                        updateModel.setStatus((byte) 1);
                        smsMapper.updateByPrimaryKeySelective(updateModel);
                    }
                }
            };
            executorService.execute(syncRunnable);
        }
        return true;
    }
}
